package org.algorithm.YaYa.数组.三月.第三周;

import java.util.*;

/**
 * @version 1.0
 * @date 2022/03/16 9:08 PM
 * @description:
 *
 *
 * url: https://leetcode-cn.com/problems/frequency-of-the-most-frequent-element/
 */
public class 最高频元素的频数 {

    public static void main(String[] args) {
        int[] nums = {1,4,8,13};
        System.out.println(maxFrequency(nums, 5));
    }

    public static int maxFrequency(int[] nums, int k){

        int n = nums.length;
        Map<Integer, Integer> map = new HashMap<>();
        for (int i : nums) map.put(i, map.getOrDefault(i, 0) + 1);
        List<Integer> list = new ArrayList<>(map.keySet());
        Collections.sort(list);
        int ans = 1;
        for (int i = 0; i < list.size(); i++) {
            int x = list.get(i), cnt = map.get(x);
            if (i > 0) {
                int p = k;
                for (int j = i - 1; j >= 0; j--) {
                    int y = list.get(j);
                    int diff = x - y;
                    if (p >= diff) {
                        int add = p / diff;
                        int min = Math.min(map.get(y), add);
                        p -= min * diff;
                        cnt += min;
                    } else {
                        break;
                    }
                }
            }
            ans = Math.max(ans, cnt);
        }
        return ans;
    }
}
